
# Tests for treelang; run from gcc/treelang/Make-lang.in => gcc/Makefile
# find ttt for the actual tests

# Copyright (C) 1999, 2000, 2001, 2002 by The Free Software Foundation

# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 2, or (at your option) any
# later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.
#
# In other words, you are welcome to use, share and improve this program.
# You are forbidden to forbid anyone else to use, share and improve
# what you give them.   Help stamp out software-hoarding!  

# Check the pgm is even there and set up the basics
proc init_utility {pgm} {
    global transform
    global pgm_actual
    global pgm_base
    global fix_progname
    global path
# maybe add "X" to front of fail to say it is an expected failure
    global X

    set pgm_base ${pgm}
    set pgm_actual ${pgm}

    if { ${transform} != "s,x,x,"} {
        verbose "1. program name was ${pgm}" 2
        set sed_rc [catch {eval exec sed -e "${transform}" <<${pgm} } catch_res]
        if { ${sed_rc} != "0" } {
            verbose "2. Program name transform failed rc=${sed_rc} stat=${catch_res}" 1
            ${X}fail "${pgm} sed"
            return 0
        }
        set pgm_actual ${catch_res}
        verbose "3. program name after transformation is ${pgm_actual}" 2
    }

    set which_rc [catch {exec which ${pgm_actual}} stat]
    if { ${which_rc} != "0" } {
        verbose "4. ${pgm_base} cannot be found rc=${which_rc} stat=${stat}" 1
        ${X}fail "${pgm} = ${pgm_actual} not found in path (${path})"
        return 0
    }
    set fix_progname "s,${pgm_actual},${pgm_base},"
    verbose "5. fix program name value = ${fix_progname}" 4
    return 1
}

#run pgm, option to remove file names from outputs
proc run3 {srcdd testdd parms group_nbr item_nbr nonzero_RC_expected check_file sanitize_output tree1 pipe} {

    global transform
    global pgm_actual
    global pgm_base
    global fix_progname
    global X
    global extras

    set error_msg 0
    set basefile "a${group_nbr}${pgm_base}.out${item_nbr}"
    set infile  ""
    set outfile  ""
    set suffix ""
    set temp_extras "-O3 "
    set real_pgm_actual ${pgm_actual}

    if  {${tree1} > 0} {
        if {"${pgm_actual}" == "gcc"} {
            set real_pgm_actual "xgcc"
            set temp_extras "${extras}"
        }
        set infile "${srcdd}/a${group_nbr}${pgm_base}i${item_nbr}.tree"
        set mainfile "${srcdd}/a${group_nbr}${pgm_base}i${item_nbr}.c"
        set outfile "-o ${testdd}/a${group_nbr}${pgm_base}o${item_nbr}${suffix}"
    }
    
    verbose "6. exec ${real_pgm_actual} ${temp_extras} ${parms} ${mainfile} ${infile} ${outfile} >${testdd}/${basefile} 2>${testdd}/${basefile}err" 2
    set run_rc [catch {eval exec ${real_pgm_actual} ${temp_extras} ${parms} ${mainfile} ${infile} ${outfile} >${testdd}/${basefile} 2>${testdd}/${basefile}err} catch_res]
    if {${run_rc} == 1} {
        if {${nonzero_RC_expected} == 0} {
            verbose "7. ${real_pgm_actual} ${group_nbr} ${item_nbr} failed due to rc=${run_rc} status=${catch_res}" 1
            ${X}fail "${pgm_base} ${group_nbr} ${item_nbr} rc!=0" 
            return
        } 
    } else {
        if {${nonzero_RC_expected} == 1} {
            verbose "8. ${pgm_actual} ${group_nbr} ${item_nbr} failed - did not produce nonzero return code as expected rc=${run_rc} status=${catch_res}" 1
            ${X}fail "${pgm_base} ${group_nbr} ${item_nbr} rc=0" 
            return
        } 
    }

# change the filenames to (file) in output if needed to allow testing
    set checkfile1 "${srcdd}/${basefile}"
    set checkfile2 "${testdd}/${basefile}"
    if {${sanitize_output} != 0} {
        set oldcheckfile1 "${checkfile1}"
        set oldcheckfile2 "${checkfile2}"
        set checkfile1 "${testdd}/${basefile}.test.nofilename"
        set checkfile2 "${testdd}/${basefile}.run.nofilename"
        set run_rc [catch {eval exec sed -f ${srcdd}/filefix.sed <${oldcheckfile1} >${checkfile1}} catch_res]
        if {${run_rc} == 1} {
            verbose "9. sed to cleanup filenames (std 1) in pgm output failed due to rc=${run_rc} status=${catch_res}" 1
            if  {${error_msg} == 0} {
                set error_msg "9. sed to cleanup filenames (std 1) in pgm output failed due to rc=${run_rc} status=${catch_res}"
            }
        }
        set run_rc [catch {eval exec sed -f ${srcdd}/filefix.sed <${oldcheckfile2} | sed -e  "${fix_progname}"  >${checkfile2}} catch_res]
        if {${run_rc} == 1} {
            verbose "10. sed to cleanup filenames (std 2) in pgm output failed due to rc=${run_rc} status=${catch_res}" 1
            if  {${error_msg} == 0} {
                set error_msg "10. sed to cleanup filenames (std 2) in pgm output failed due to rc=${run_rc} status=${catch_res}" 
            }
        }
    }
    set diff [diff ${checkfile1} ${checkfile2}]
    if {${diff} != 1} {
        verbose "11. ${pgm_actual} ${group_nbr} ${item_nbr} diff stdout failed rc=${diff}" 1
        if  {${error_msg} == 0} {
            set error_msg "11. ${pgm_actual} ${group_nbr} ${item_nbr} diff stdout failed rc=${diff}"
        }
    }

    set checkfile1 "${srcdd}/${basefile}err"
    set checkfile2 "${testdd}/${basefile}err"
    if {${sanitize_output} != 0} {
        set oldcheckfile1 "${checkfile1}"
        set oldcheckfile2 "${checkfile2}"
        set checkfile1 "${testdd}/${basefile}err.test.nofilename"
        set checkfile2 "${testdd}/${basefile}err.run.nofilename"
        set run_rc [catch {eval exec sed -f ${srcdd}/filefix.sed <${oldcheckfile1} >${checkfile1}} catch_res]
        if {${run_rc} == 1} {
            verbose "12. sed to cleanup filenames (err 1) in pgm output failed due to rc=${run_rc} status=${catch_res}" 1
            if  {${error_msg} == 0} {
                set error_msg "12. sed to cleanup filenames (err 1) in pgm output failed due to rc=${run_rc} status=${catch_res}"
            }
        }
        set run_rc [catch {eval exec sed -f ${srcdd}/filefix.sed <${oldcheckfile2} | sed -e  "${fix_progname}"  >${checkfile2}} catch_res]
        if {${run_rc} == 1} {
            verbose "13. sed to cleanup filenames (err 2) in pgm output failed due to rc=${run_rc} status=${catch_res}" 1
            if  {${error_msg} == 0} {
                set error_msg "13. sed to cleanup filenames (err 2) in pgm output failed due to rc=${run_rc} status=${catch_res}"
            }
        }
    }
    set diff [diff ${checkfile1} ${checkfile2}]
    if {${diff} != 1} {
        verbose "14. ${pgm_actual} ${group_nbr} ${item_nbr} diff stderr failed rc=${diff}" 1
        if  {${error_msg} == 0} {
            set error_msg "14. ${pgm_actual} ${group_nbr} ${item_nbr} diff stderr failed rc=${diff}"
        }
    }

    if {${check_file} >0} {
        if  {${tree1} == 0} {
            set checkfile1 "${srcdd}/${basefile}file"
            set checkfile2 "${testdd}/${basefile}file"
            if {${sanitize_output} != 0} {
                set oldcheckfile1 "${checkfile1}"
                set oldcheckfile2 "${checkfile2}"
                set checkfile1 "${testdd}/${basefile}file.test.nofilename"
                set checkfile2 "${testdd}/${basefile}file.run.nofilename"
                set run_rc [catch {eval exec sed -f ${srcdd}/filefix.sed <${oldcheckfile1} >${checkfile1}} catch_res]
                if {${run_rc} == 1} {
                    verbose "15. sed to cleanup filenames (err 1) in pgm stdout failed due to rc=${run_rc} status=${catch_res}" 1
                    if  {${error_msg} == 0} {
                        set error_msg "15. sed to cleanup filenames (err 1) in pgm stdout failed due to rc=${run_rc} status=${catch_res}"
                    }
                }
                set run_rc [catch {eval exec sed -f ${srcdd}/filefix.sed <${oldcheckfile2} | sed -e  "${fix_progname}"  >${checkfile2}} catch_res]
                if {${run_rc} == 1} {
                    verbose "16. sed to cleanup filenames (err 2) in pgm stdout failed due to rc=${run_rc} status=${catch_res}" 1
                    if  {${error_msg} == 0} {
                        set error_msg "16. sed to cleanup filenames (err 2) in pgm stdout failed due to rc=${run_rc} status=${catch_res}"
                    }
                }
            }
            set diff [diff ${checkfile1} ${checkfile2}]
            if {${diff} != 1} {
                verbose "17. ${pgm_actual} ${group_nbr} ${item_nbr} diff stdout file failed rc=${diff}" 1
                if  {${error_msg} == 0} {
                    set error_msg "17. ${pgm_actual} ${group_nbr} ${item_nbr} diff stdout file failed rc=${diff}"
                }
            }
        }
    }
    
    if {${check_file} >1} {
        if  {${tree1} == 0} {
            set checkfile1 "${srcdd}/${outfile}"
            set checkfile2 "${testdd}/${outfile}"
            if {${sanitize_output} != 0} {
                set oldcheckfile1 "${checkfile1}"
                set oldcheckfile2 "${checkfile2}"
                set checkfile1 "${testdd}/${basefile}out.test.nofilename"
                set checkfile2 "${testdd}/${basefile}out.run.nofilename"
                set run_rc [catch {eval exec sed -f ${srcdd}/filefix.sed <${oldcheckfile1} >${checkfile1}} catch_res]
                if {${run_rc} == 1} {
                    verbose "18. sed to cleanup filenames (err 1) in pgm output failed due to rc=${run_rc} status=${catch_res}" 1
                    if  {${error_msg} == 0} {
                        set error_msg "18. sed to cleanup filenames (err 1) in pgm output failed due to rc=${run_rc} status=${catch_res}"
                    }
                }
                set run_rc [catch {eval exec sed -f ${srcdd}/filefix.sed <${oldcheckfile2} | sed -e  "${fix_progname}"  >${checkfile2}} catch_res]
                if {${run_rc} == 1} {
                    verbose "19. sed to cleanup filenames (err 2) in pgm output failed due to rc=${run_rc} status=${catch_res}" 1
                    if  {${error_msg} == 0} {
                        set error_msg "19. sed to cleanup filenames (err 2) in pgm output failed due to rc=${run_rc} status=${catch_res}"
                    }
                }
            }
            set diff [diff ${checkfile1} ${checkfile2}]
            if {${diff} != 1} {
                verbose "20. ${pgm_actual} ${group_nbr} ${item_nbr} diff output file failed rc=${diff}" 1
                if  {${error_msg} == 0} {
                    set error_msg "20. ${pgm_actual} ${group_nbr} ${item_nbr} diff output file failed rc=${diff}"
                }
            }
        }
    }
    
    if {${check_file} >2} {
        set outfile "a${group_nbr}${pgm_base}o${item_nbr}${suffix}"
        set pgmrun "${testdd}/a${group_nbr}${pgm_base}o${item_nbr}${suffix}"
        set checkfile1 "${srcdd}/${outfile}runpgm"
        set checkfile2 "${testdd}/${outfile}runpgm"
        verbose "21. exec ${pgmrun} >${checkfile2} 2>${checkfile2}err" 2
        set run_rc [catch {eval exec ${pgmrun} >${checkfile2} 2>${checkfile2}err} catch_res]
        if {${run_rc} == 1} {
            if {${nonzero_RC_expected} == 0} {
                verbose "22. ${pgm_actual} ${group_nbr} ${item_nbr} failed due to rc=${run_rc} status=${catch_res}" 1
                ${X}fail "${pgm_base} ${group_nbr} ${item_nbr} run" 
                return
            } 
        } else {
            if {${nonzero_RC_expected} == 1} {
                verbose "23. ${pgm_actual} ${group_nbr} ${item_nbr} failed - did not produce nonzero return code as expected rc=${run_rc} status=${catch_res}" 1
                ${X}fail "${pgm_base} ${group_nbr} ${item_nbr} run" 
                return
            } 
        }
        set diff [diff ${checkfile1} ${checkfile2}]
        if {${diff} != 1} {
            verbose "24. ${pgm_actual} ${group_nbr} ${item_nbr} diff run output file failed rc=${diff}" 1
            if  {${error_msg} == 0} {
                set error_msg "24. ${pgm_actual} ${group_nbr} ${item_nbr} diff run output file failed rc=${diff}"
            }
        }
        set diff [diff ${checkfile1}err ${checkfile2}err]
        if {${diff} != 1} {
            verbose "25. ${pgm_actual} ${group_nbr} ${item_nbr} diff run stderr file failed rc=${diff}" 1
            if  {${error_msg} == 0} {
                set error_msg "25. ${pgm_actual} ${group_nbr} ${item_nbr} diff run stderr file failed rc=${diff}"
            }
        }
    }

    if {${error_msg}>0} {
        ${X}fail "${pgm_base} ${group_nbr} ${item_nbr} fail code=${error_msg}" 
    } else {
        pass "${pgm_base} ${group_nbr} ${item_nbr}" 
    }
    return 
}

set extras "$env(gcc_extras)"
set path $env(PATH)
set transform $env(TRANSFORM)
set srcdir $env(srcdir)
verbose "source directory ${srcdir}\n" 2
verbose "transform ${transform}\n" 2
set sourcedir "${srcdir}/testsuite/treelang"
set testdir "treelang"

set pgm_actual ""

# srcdd testdd parms group_nbr item_nbr nonzero_RC_expected check_file sanitize_output tree1

# ttt

#GCC - main C compiler tests via GCC

set X ""

set check_rc [init_utility "gcc"]

if {${check_rc} == 1} {
#
#set X "x"
set X ""
    run3 "${sourcedir}" "${testdir}" " -g -O3 " 01 01 0 3 0 1 0
set X ""
}

